#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
using namespace std;
int n = 0, m = 0;
int a[] = { 0,0,1,-1 };
int b[] = { 1,-1,0,0 };
int recursion(vector<vector<int>>& vv, int i, int j, vector<vector<int>>& dp)
{
    int Max = 0;
    for (int k = 0;k < 4;k++)
    {
        int x = i + a[k];
        int y = j + b[k];
        int len = 0;
        if ((x >= 0 && x < n) && (y >= 0 && y < m) && vv[x][y] < vv[i][j])
        {
            if (dp[x][y] > 0) len = dp[x][y];
            else len = recursion(vv, x, y, dp);
            Max = max(Max, len);
        }
    }
    dp[i][j] = Max + 1;
    return Max + 1;
}
int main()
{
    cin >> n >> m;
    vector<vector<int>> vv(n, vector<int>(m, 0));
    for (int i = 0;i < n;i++)
    {
        for (int j = 0;j < m;j++)
        {
            cin >> vv[i][j];
        }
    }
    int Max = 0;
    vector<vector<int>> dp(n, vector<int>(m, 0));
    for (int i = 0;i < n;i++)
    {
        for (int j = 0;j < m;j++)
        {
            Max = max(Max, recursion(vv, i, j, dp));
        }
    }
    cout << Max << endl;
}